package com.siwei.apply.service.impl;

import com.siwei.apply.domain.Jsydghxk;
import com.siwei.apply.domain.Project;
import com.siwei.apply.domain.res.JsydghxkRes;
import com.siwei.apply.domain.vo.JsydghxkUpdateVo;
import com.siwei.apply.domain.vo.JsydghxkVo;
import com.siwei.apply.mapper.JsydghxkMapper;
import com.siwei.apply.mapper.ProjectMapper;
import com.siwei.apply.service.JsydghxkService;
import com.siwei.common.core.utils.bean.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.siwei.apply.domain.ProjectWorkflow;
import com.siwei.apply.domain.Workflow;
import com.siwei.apply.mapper.ProjectWorkflowMapper;
import com.siwei.apply.mapper.WorkflowMapper;

import java.util.List;

import static com.siwei.apply.common.Common.UserId;

/**
 * 建设用地规划许可 服务实现类
 */
@Service
public class JsydghxkImpl implements JsydghxkService {
    @Autowired
    private JsydghxkMapper jsydghxkMapper;
    @Autowired
    private ProjectMapper projectMapper;
    @Autowired
    private ProjectWorkflowMapper projectWorkflowMapper;
    @Autowired
    private WorkflowMapper workflowMapper;

    @Override
    public Boolean isExit(String projectId) {
        return jsydghxkMapper.isExit(projectId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String add(JsydghxkVo jsydghxkVo) {
        Jsydghxk jsydghxk = new Jsydghxk();
        BeanUtils.copyProperties(jsydghxkVo, jsydghxk);
        jsydghxk.generateId();
        jsydghxk.setCreatorId(UserId);
        jsydghxkMapper.add(jsydghxk);

        Integer projectType = projectMapper.getProjectTypeById(jsydghxk.getProjectId());
        Workflow workflow = workflowMapper.selectByProjectTypeAndTableName(projectType, "t_jsydghxk");
        Workflow workflowPrev = workflowMapper.selectPrevByProjectTypeAndTableName(projectType, "t_jsydghxk");
        String parentId = "";
        if (workflowPrev != null) {
            List<ProjectWorkflow> projectWorkflows = projectWorkflowMapper.selectByProjectIdAndNodeTableName(jsydghxk.getProjectId(), workflowPrev.getTableName());
            if (projectWorkflows != null && !projectWorkflows.isEmpty()) {
                parentId = projectWorkflows.get(0).getNodeId();
            }
        }
        ProjectWorkflow projectWorkflow = new ProjectWorkflow();
        projectWorkflow.setId(jsydghxk.getId());
        projectWorkflow.setParentId(parentId);
        projectWorkflow.setWorkflowId(workflow.getId());
        projectWorkflow.setProjectId(jsydghxk.getProjectId());
        projectWorkflow.setNodeId(jsydghxk.getId());
        projectWorkflow.setNodeTableName(workflow.getTableName());
        projectWorkflow.setWorkflowName(workflow.getName());
        projectWorkflowMapper.add(projectWorkflow);
        return jsydghxk.getId();
    }

    @Override
    public JsydghxkRes get(String projectId) {
        Jsydghxk jsydghxk = jsydghxkMapper.get(projectId);
        Project project = projectMapper.get(projectId);

        JsydghxkRes jsydghxkRes = new JsydghxkRes();
        if (jsydghxk != null) {
            BeanUtils.copyProperties(jsydghxk, jsydghxkRes);
        }
        jsydghxkRes.setProjectId(projectId);
        jsydghxkRes.setProjectName(project.getName());
        jsydghxkRes.setProjectCode(project.getCode());
        return jsydghxkRes;
    }

    @Override
    public JsydghxkRes getById(String id) {
        Jsydghxk record = jsydghxkMapper.getById(id);
        if (record == null) {
            return null;
        }
        Project project = projectMapper.get(record.getProjectId());
        JsydghxkRes res = new JsydghxkRes();
        BeanUtils.copyProperties(record, res);
        res.setProjectId(record.getProjectId());
        res.setProjectName(project.getName());
        res.setProjectCode(project.getCode());
        return res;
    }

    @Override
    public void update(JsydghxkUpdateVo jsydghxkUpdateVo) {
        jsydghxkMapper.update(jsydghxkUpdateVo);
    }

    @Override
    public void updateHasOnchain(String id, Boolean hasOnchain) {
        jsydghxkMapper.updateHasOnchain(id, hasOnchain);
    }
}
